package com.hotel.product.service.impl;

import cn.hutool.core.convert.NumberWithFormat;
import com.hotel.common.config.SystemConfig;
import com.hotel.common.util.JwtUtil;
import com.hotel.common.vo.R;
import com.hotel.product.entity.LevelReq;
import com.hotel.user.entity.UserLevel;
import com.hotel.product.mapper.UserLevelMapper;
import com.hotel.product.service.UserLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * <p>
 * 用户积分表 服务实现类
 * </p>
 *
 * @author KK
 * @since 2024-06-18
 */
@Service
public class UserLevelServiceImpl extends ServiceImpl<UserLevelMapper, UserLevel> implements UserLevelService {

    @Autowired
    private UserLevelMapper userLevelMapper;

    @Override
    public R getUserPoints(HttpServletRequest request) {
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        int id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        UserLevel userLevel = userLevelMapper.selectById(id);
        Integer growths = userLevel.getGrowths();
        LevelReq levelReq = new LevelReq();
        levelReq.setGrowths(growths);
        return R.ok(levelReq);
    }
}
